package com.newproject.resmi;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;

public class TabView extends TabActivity {
	private static final String USER_DETAILS = "UserPrefs";
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);

	    Resources res = getResources(); // Resource object to get Drawables
	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Reusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab

	    intent = new Intent().setClass(this, StocksGrid.class);
	    spec = tabHost.newTabSpec("stocks").setIndicator(this.getString(R.string.stocks),
	                      res.getDrawable(R.drawable.ic_tab_stocks))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    intent = new Intent().setClass(this, Portfolio.class);
	    spec = tabHost.newTabSpec("portfolio").setIndicator(this.getString(R.string.portfolio),
	                      res.getDrawable(R.drawable.ic_tab_portfolio))
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    
	    intent = new Intent().setClass(this, Money.class);
	    spec = tabHost.newTabSpec("money").setIndicator(this.getString(R.string.money),
	                      res.getDrawable(R.drawable.ic_tab_money))
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    tabHost.setCurrentTab(0);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.settings:
			Intent newIntent = new Intent(TabView.this, UserPreferences.class);
			TabView.this.startActivity(newIntent);
			return true;
		case R.id.log_out:
			SharedPreferences settings = getSharedPreferences(USER_DETAILS, Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = settings.edit();
			editor.remove("clientId");
		    editor.commit();
		    TabView.this.finish();
		    return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();		
		if (!isOnline()) {
			AlertDialog.Builder builder = new AlertDialog.Builder(TabView.this);
    		builder.setMessage(getString(R.string.no_connection))
    		       .setCancelable(false)
    		       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
    		           public void onClick(DialogInterface dialog, int id) {
    		                TabView.this.finish();
    		           }
    		       });
    		AlertDialog alert = builder.create();
    		alert.show();
		}
	}
	
	/**
	 * Check if a device is connected to a network
	 */
	public boolean isOnline() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm.getActiveNetworkInfo() != null) {
			return cm.getActiveNetworkInfo().isConnectedOrConnecting();
		}		
		return false;
	}

}
